/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Servlet;

import com.sun.rowset.CachedRowSetImpl;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.io.*;
import java.sql.Connection;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import javax.sql.rowset.CachedRowSet;

/**
 *
 * @author 50920709
 */
public class SelectAppletData extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
           /* out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet AppletData</title>");  
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet AppletData at " + request.getContextPath () + "</h1>");
            out.println("</body>");
            out.println("</html>");
            */
            out.println("testsql: " + testsql);
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        	try {
			response.setContentType("application/x-java-serialized-object");

			// read a String-object from applet
			// instead of a String-object, you can transmit any object, which
			// is known to the servlet and to the applet
			InputStream in = request.getInputStream();
			ObjectInputStream inputFromApplet = new ObjectInputStream(in);
			String sql = (String) inputFromApplet.readObject();

            testsql = sql;

            /* Connect to DB*/
            String className = "com.mysql.jdbc.Driver";
            String  connectionURL = "jdbc:mysql://144.214.121.25:3306/websudoku?" + "user=root&password=nbuser";
            Connection  con = null;
            Statement stmt = null;
            ResultSet rs = null;
            CachedRowSet rowset = new CachedRowSetImpl();

            try{
                Class.forName(className).newInstance();
                con = DriverManager.getConnection(connectionURL);
                stmt = con.createStatement();
                rs = stmt.executeQuery(sql);
                rowset.populate(rs);
                rs.close();
                stmt.close();
                con.close();
            }catch(Exception ex){
            }

			// echo it to the applet
			OutputStream outstr = response.getOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(outstr);
			oos.writeObject(rowset);
			oos.flush();
			oos.close();

		} catch (Exception e) {
			e.printStackTrace();
		}
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private String testsql;

}
